package com.iwhalecloud.bss.kite.cucc.service.attr.handler;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Resource;

import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegionQry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IUnicomRegionService;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.context.CuccServiceContext;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.ContextUtil;

/**
 * SDH订购-地市属性
 */
public class LocalZxCityAttrHandler  extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Resource
    private IUnicomRegionService unicomRegionService;

    @Override
    public void init(KiteAttr attr, KiteInst inst) {
        super.init(attr, inst);
    }

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {

        //重提初始化不校验
        //fixme 可能导致频繁访问缓存,上线临时处理方案
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (Objects.nonNull(extParams)&& extParams.containsKey("nodeCode")) {
            return;
        }

        // 变更业务的场景初始化过程中不校验
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(
            querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE_SCENE))
            || KiteStringUtils.equals(attr.getOldValue(), value)) {
            return;
        }


        //电路租用范围
         String eRentScopeAttrValue = inst.getAttrValue("2_0000004");
         String otherFieldName = KiteStringUtils.equals(attr.getFieldName(), "2_0000221")?"2_0000109":"2_0000221";
         String otherValue = inst.getAttrValue(otherFieldName);
         if (KiteStringUtils.isAnyEmpty(eRentScopeAttrValue,value,otherValue)){
             return;
         }
    }

    /**
     * 获取页面展示名称
     *
     * @param attr 实例属性入参
     * @param inst 当前操作的实例对象
     * @param value 设置的值
     * @param data 属性处理入参，可以根据需要传入需要的业务数据
     * @return 属性页面展示名称
     */
    @Override
    public String getValueDesc(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        if (KiteStringUtils.isEmpty(value)) {
            return value;
        }
        StopWatch sw = new StopWatch("LocalZxCityAttrHandler");
        sw.start("LocalZxCityAttrHandler");
        String parentAreaCode = inst.getAttrValue(getProvinceFieldName(attr.getFieldName()));
        String key = KiteStringUtils.concatWithNnderline(parentAreaCode, inst.getSpecId());
        List<UnicomRegion> unicomRegions = CuccServiceContext.getRegions(key);
        if (KiteListUtils.isEmpty(unicomRegions)) {
            unicomRegions = unicomRegionService.subRegionFromPlatform(new UnicomRegionQry(parentAreaCode, "20", inst.getSpecId()));
            CuccServiceContext.setRegions(key, unicomRegions);
        }
        sw.stop();
        System.out.println(sw.prettyPrint());
        for (UnicomRegion unicomRegion : unicomRegions) {
            if(value.equals(unicomRegion.getRegionCode())) {
                return unicomRegion.getRegionName();
            }
        }
        return "";
    }

    /**
     * 根据地市字段的fieldName获取省字段的fieldName
     */
    private static String getProvinceFieldName(String cityFieldName) {
        return KiteStringUtils.isEqual(cityFieldName, "2_0000109") ? "2_0000107" : "2_0000220";
    }
}
